/*
 * Copyright (C) 2013 Buptcoder
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.buptcoder.wifimap;

import com.buptcoder.wifimap.Utils.Constants;
import com.buptcoder.wifimap.Utils.MapLog;
import com.buptcoder.wifimap.wifiinfo.WifiAccessPointInfo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SingleAPMapActivity extends Activity implements
        View.OnClickListener {
    private static final String TAG = "SingleAPMapActivity";
    private WebView mMapView;
    private WifiAccessPointInfo mAPInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.map_view);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.titlebar_with_back);
        initData();
        setTitle();
        initWebandWebSettings();
        mMapView.loadUrl("file:///android_asset/mapforsingleap.html");
    }

    private void initData() {
        Intent intent = getIntent();
        double lat = intent.getDoubleExtra(Constants.APINFO_LAT_KEY, 0);
        double lon = intent.getDoubleExtra(Constants.APINFO_LON_KEY, 0);
        String ssid = intent.getExtras().getString(Constants.APINFO_SSID_KEY);
        int type = intent.getIntExtra(Constants.APINFO_TYPE_KEY, 0);
        mAPInfo = new WifiAccessPointInfo();
        mAPInfo.setLantitude(lat);
        mAPInfo.setLongitude(lon);
        mAPInfo.setSSID(ssid);
        mAPInfo.setType(type);
        MapLog.d(TAG, "Type is " + type);
    }

    private void setTitle() {
        TextView textView = (TextView) findViewById(R.id.titlebar_title);
        textView.setText(mAPInfo.getSSID());
        Button backButton = (Button) findViewById(R.id.titlebar_back_btn);
        Button stopButton = (Button) findViewById(R.id.title_stop_record);
        backButton.setOnClickListener(this);
        stopButton.setVisibility(View.INVISIBLE);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebandWebSettings() {
        mMapView = (WebView) this.findViewById(R.id.map_view);
        mMapView.setWebChromeClient(new MyWebChromeClient());

        WebSettings webSettings = mMapView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        addJavaScriptInterfaces();
    }

    private void addJavaScriptInterfaces() {
        addAPPoiObject();
    }

    private void addAPPoiObject() {
        mMapView.addJavascriptInterface(mAPInfo, "APInfo");
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.titlebar_back_btn) {
            onBackPressed();
        }
    }

    // For debug
    class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message,
                JsResult result) {

            Toast.makeText(SingleAPMapActivity.this, message,
                    Toast.LENGTH_SHORT).show();
            MapLog.d(TAG, "Error happened: " + message);
            return false;
        }
    }

}
